package com.party.web.biz.dynamic;

import com.party.core.model.circle.Circle;
import com.party.core.model.circle.CircleTopic;
import com.party.core.model.commune.MomentType;
import com.party.core.model.dynamic.Dynamic;
import com.party.core.model.dynamic.DynamicType;
import com.party.core.model.dynamic.Dypics;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.circle.ICircleService;
import com.party.core.service.circle.biz.CircleTopicBizService;
import com.party.core.service.commune.ICMomentService;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.dynamic.IDynamicService;
import com.party.core.service.dynamic.IDypicsService;
import com.party.core.service.dynamic.biz.DynamicBaseBizService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.web.biz.file.FileBizService;
import com.party.web.biz.member.CMomentBizService;
import com.party.web.utils.RealmUtils;
import com.party.web.web.controller.activity.ActivityController;
import com.party.web.web.dto.output.dynamic.DynamicOutput;
import com.party.web.web.dto.output.member.CMomentSave;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DynamicBizService {

    @Autowired
    private IDypicsService dypicsService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private DynamicBaseBizService dynamicBaseBizService;

    @Autowired
    private CircleTopicBizService circleTopicBizService;

    @Autowired
    private FileBizService fileBizService;

    @Autowired
    private CosBizService cosBizService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IDynamicService dynamicService;
    @Autowired
    private CMomentBizService momentBizService;
    @Autowired
    private ICircleService circleService;

    protected static Logger logger = LoggerFactory.getLogger(DynamicBizService.class);

    public DynamicOutput transformCommon(Dynamic input){
        DynamicOutput output = DynamicOutput.transform(input);
        // 动态图片列表
        List<Dypics> dypics = dypicsService.findByDynamicId(input.getId(), null);
        output.setPicList(dypics);
        // 点赞数
        output.setCommentNum(input.getCommentNum());
        // 评论数
        output.setLoveNum(input.getLoveNum());

        String filePath = fileBizService.getUploadCIPath("dynamic", input.getCreateBy());
        String qrCodeUrl = cosBizService.getPreviewPictureUrl(filePath + input.getId());
        output.setQrCodeUrl(qrCodeUrl);
        // 动态发布者
        Member authorMember = memberService.get(input.getCreateBy());
        if (null == authorMember) {
            MemberGroup memberGroup = memberGroupService.get(input.getCreateBy());
            if (null != memberGroup) {
                output.setMemberName(memberGroup.getRealname());
                output.setMemberLogo(memberGroup.getLogo());
            }
        } else {
            output.setMemberLogo(authorMember.getLogo());
            output.setMemberName(authorMember.getRealname());
        }
        return output;
    }

    /**
     * 编辑或保存话题
     *
     * @param entity
     * @throws Exception
     */
    @Transactional
    public void saveBizTopic(Dynamic entity, CircleTopic topic) throws Exception {
        entity.setDynamicType(DynamicType.CIRCLE.getCode());
        String dynamicId = dynamicBaseBizService.saveBizDynamic(entity, RealmUtils.getCurrentUser().getId());
        try {
            Circle circle = circleService.get(topic.getCircle());
            CMomentSave cMomentSave = new CMomentSave();
            cMomentSave.setPartnerId(circle.getCreateBy());
            cMomentSave.setBizId(entity.getId());
            cMomentSave.setDynamic(entity);
            cMomentSave.setMomentType(MomentType.TOPIC_PUBLISH.getCode());
            momentBizService.businessSaveCMoment(cMomentSave);
        }catch (Exception e){
            logger.error("发布活动同时发布动态--失败");
        }

        topic.setDynamic(dynamicId);
        circleTopicBizService.saveTopic(topic,RealmUtils.getCurrentUser().getId());
        try {
            Dynamic dynamic = dynamicService.get(dynamicId);
            String filePath = fileBizService.getUploadCIPath("dynamic", dynamic.getCreateBy());
            String fileName = dynamic.getId();
            String content = "community/dynamic_detail.html?id=" + dynamic.getId();
            fileBizService.uploadCloudOut(fileName,filePath,content,dynamic.getCreateBy());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
